import {
  postLoginApi,
  postLikesApi,
  getLikesApi,
  getLikesCountApi,
  postStudyApi,
  getStudyBdidByUidApi,
  delLikeApi,
  postCollectionApi,
  getCollectionApi,
  getMsbdByCollectApi,
  deleteCollectApi
} from '../../api/login'
import {
  setData,
  getData
} from '../../static/session/setForm'

export default {
  namespaced: true,
  state: {
    form: JSON.parse(getData() || '{}'),
    likesData: {},
    styleList: [],
    total: 0,
    collections: {},
    collect: []
  },
  mutations: {
    SET_FORM(state, form) {
      state.form = form
    },
    SET_LIKE(state, like) {
      state.likesData = like
    },
    SET_STUDY_BDID(state, styleList) {
      state.styleList = styleList
    },
    SET_TOTAL(state, total) {
      state.total = total
    },
    SET_COLLECTION(state, collection) {
      state.collections = collection
    },
    SET_COLLECT(state, collect) {
      state.collect = collect
    }
  },
  actions: {
    async postLogin({
      commit
    }, form) {
      let r = await postLoginApi(form.form)
      commit('SET_FORM', r.data.data[0])
      // console.log(r.data.data[0])
      setData(r.data.data[0])
    },
    async postLikes({
      commit
    }, likesId) {
      let r = await postLikesApi({
        uid: likesId.uid,
        bdid: likesId.bdid
      })
      // console.log(r)
    },
    async getLikesData({
      commit,
      rootState
    }, {
      bdid
    }) {
      // let bdid = rootState.discuss.itemId.id
      let uid = rootState.login.form.id
      let r = await getLikesApi({
        bdid,
        uid
      })
      //  console.log(r.data.data)
      // let b = r.data.data.map(i => i.bdid)
      commit('SET_LIKE', r.data.data[0])
      // console.log(r.data.data[0])
      commit('SET_TOTAL', r.data)
      // console.log(r.data)
    },
    async likeCount({
      commit
    }, bdid) {
      let r = await getLikesCountApi(bdid)
      // console.log(r)
      // console.log(bdid)
    },
    async postStudy({
      commit,
      rootState
    }, data) {
      let bdid = rootState.discuss.itemId.id
      // console.log(rootState.login.form.id)
      await postStudyApi({
        uid: data.uid,
        bdid
      })
    },
    async getStudyBdid({
      commit,
      rootState
    }) {
      // console.log(rootState)
      let uid = rootState.login.form.id
      let r = await getStudyBdidByUidApi({
        uid
      })
      commit('SET_STUDY_BDID', r.data.data)
      //  console.log(r.data.data[0])
    },
    async delLikes({
      commit,
      rootState
    }, {
      bdid
    }) {
      let uid = rootState.login.form.id
      // let bdid = rootState.discuss.itemId.id
      // console.log(uid,bdid)
      await delLikeApi({
        uid,
        bdid
      })
    },
    async postCollection({
      commit
    }, data) {
      await postCollectionApi({
        bdid: data.bdid,
        uid: data.uid
      })
    },
    async getCollection({
      commit
    }, data) {
      let r = await getCollectionApi({
        uid: data.uid
      })
      commit('SET_COLLECTION', r.data.data)
      // console.log(r.data.data)
    },
    async getMsbdByCollect({
      commit
    }, data) {
      let r = await getMsbdByCollectApi({
        uid: data.uid
      })
      commit('SET_COLLECT', r.data.data)
      // console.log(r)
    },
    async deleteCollect({
      commit
    }, data) {
      await deleteCollectApi({
        uid: data.uid,
        bdid: data.bdid
      })
    }
    // async postLogout({commit}){
    //   await postLogoutApi()
    // }
  },
  getters: {

  }
}